package concurrency.guava;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.Service;
import example.avro.User;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * User: belict
 * Date: 12/1/13
 * Time: 2:53 PM
 * To change this template use File | Settings | File Templates.
 */
public class Run {
    //Awesome
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        SomeClass someClass = new SomeClass();

        for (int i = 0; i < 100; i++) {
            if (someClass.state() == Service.State.NEW)
                someClass.startAsync();
            Thread.sleep(100);
            System.out.println("Here we continue to work!!");
        }

    }

    private static void listenableFutureDemo() throws InterruptedException, ExecutionException {
        ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
        ListenableFuture<User> userFuture = service.submit(new Callable<User>() {
            @Override
            public User call() throws Exception {
                System.out.println("Fuck yeah!");
                return new User("Alex",27,"blue");
            }
        });
        userFuture.addListener(new Runnable() {
            @Override
            public void run() {
                System.out.println("userFuture completed!");
            }
        },service);

        System.out.println(userFuture.get().toString());
    }
}
